package cn.star.jots.dict.service.impl;

import cn.star.jots.dict.mapper.DictMapper;
import cn.star.jots.dict.pojo.dto.DictQuery;
import cn.star.jots.dict.pojo.dto.DictSaveParam;
import cn.star.jots.dict.pojo.entity.Dict;
import cn.star.jots.dict.pojo.vo.DictVO;
import cn.star.jots.dict.service.DictService;
import cn.star.jots.dictoption.mapper.DictOptionMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class DictServiceImpl implements DictService {
    @Autowired
    DictMapper dictMapper;
    @Autowired
    DictOptionMapper dictOptionMapper;
    @Override
    public List<DictVO> selectAll(DictQuery dictQuery) {
        log.debug("查询字典,参数:{}", dictQuery);
        return dictMapper.selectByNameAndCode(dictQuery);
    }

    @Override
    public void save(DictSaveParam dictSaveParam) {
        log.debug("保存字典,参数:{}", dictSaveParam);
        Dict dict = new Dict();
        BeanUtils.copyProperties(dictSaveParam, dict);
        if(dict.getId()==null){
            dict.setCreateTime(new Date());
            dictMapper.insert(dict);
        } else {
            dict.setUpdateTime(new Date());
            dictMapper.update(dict);
        }

    }

    @Override
    public void delete(Long id) {
        log.debug("删除字典,参数:{}", id);
        dictMapper.delete(id);
        dictOptionMapper.deleteByDictId(id);
        log.info("删除成功");
    }
}
